using System.Collections.Generic;

namespace TaskFramework
{
    public class TaskInfo
    {
        private readonly Task _task;
        private readonly TaskEventHandler _callback;
        private readonly int _routedByTaskId;
        private readonly List<int> _routedTaskIds = new List<int>();

        public TaskInfo(Task task, TaskEventHandler callback)
        {
            _task = task;
            _callback = callback;
            _routedByTaskId = _task.TaskId;
        }

        public TaskInfo(Task task, TaskEventHandler callback, int routedByTaskId)
        {
            _task = task;
            _callback = callback;
            _routedByTaskId = routedByTaskId;
        }

        public Task Task
        {
            get { return _task; }
        }

        public TaskEventHandler TaskEventCallback
        {
            get { return _callback; }
        }

        public int RoutedCount
        {
            get { lock (this) { return _routedTaskIds.Count; } }
        }

        public int RoutedByTaskId
        {
            get { return _routedByTaskId; }
        }

        public bool IsRootTask
        {
            get { return _task.TaskId == _routedByTaskId; }
        }

        public IList<int> RoutedTaskIds
        {
            get { return _routedTaskIds.AsReadOnly(); }
        }

        internal void AddRoutedTaskId(int taskId)
        {
            lock (this) { _routedTaskIds.Add(taskId); }
        }

        /// <summary>
        /// Indicates whether a task is being transfered back to the task pool.
        /// </summary>
        internal bool TransferToPool { get; set;}
    }
}